package manager.helpers.validation;

import java.awt.event.KeyEvent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import manager.auxiliares.Configurations;
import manager.auxiliares.FocusHelper;
import manager.auxiliares.Format;

public class FormatedFieldValidator extends JFormattedTextField {

    private boolean isRequired;
    private boolean upper;
    private String validationType;
    private JLabel jLabel;

    public FormatedFieldValidator(boolean isRequired, boolean upper, String validationType, JLabel jLabel) {
        super();
        this.isRequired = isRequired;
        this.upper = upper;
        this.validationType = validationType;
        this.jLabel = jLabel;

        if (validationType.equalsIgnoreCase("date")) {
            this.setFormatterFactory(Format.getDateFormat());
            this.setText(Format.getCurrentVisualDate());
        } else if (validationType.equalsIgnoreCase("cpf")) {
            this.setFormatterFactory(Format.getCpfFormat());
        } else if (validationType.equalsIgnoreCase("cnpj")) {
            this.setFormatterFactory(Format.getCnpjFormat());
        } else if (validationType.equalsIgnoreCase("money")) {
            this.setFormatterFactory(Format.getMonataryFormat());
            this.setValue(0);
        }

        /**
         * Funcionalidade que seleciona todo o texto no evento de ganho de foco
         */
        this.addFocusListener(new java.awt.event.FocusAdapter() {

            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                ((JFormattedTextField) evt.getComponent()).selectAll();
            }

            @Override
            public void focusLost(java.awt.event.FocusEvent evt) {
                JFormattedTextField jFormattedTextField = ((JFormattedTextField) evt.getComponent());
                if (jFormattedTextField.getText().equals("")) {
                    jFormattedTextField.setText("");
                }else if(!((FormatedFieldValidator) jFormattedTextField).validarCampo()){
                    jFormattedTextField.setText("");
                }
            }
        });
        /**
         * Funcionalidade que passa para o próximo elemento com o pressionar da tecla enter
         */
        this.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                int key = evt.getKeyCode();
                FormatedFieldValidator formatedFieldValidator = ((FormatedFieldValidator) evt.getComponent());
                if (formatedFieldValidator.validarCampo()) {
                    if (key == KeyEvent.VK_ENTER) {
                        FocusHelper.goToNextFocus();
                    }
                }
            }
        });
        this.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                FormatedFieldValidator formatedFieldValidator = ((FormatedFieldValidator) evt.getComponent());
                if (formatedFieldValidator.getToolTipText() == null) {
                    formatedFieldValidator.setToolTipText(formatedFieldValidator.getToolTip());
                }
            }
        });

    }

    public String getToolTip() {
        String toolTipText = (isRequired) ? "[Obrigatório]" : "";
        toolTipText = jLabel.getText() + ((!toolTipText.equals("")) ? ":" + toolTipText : "");
        return toolTipText;
    }

    public boolean validarCampo() {
        if (isRequired) {
            String str = this.getText();
            if (validationType.equalsIgnoreCase("money")) {
                str = ((Number) this.getValue()).toString();
            }
            boolean validated = ValidationFunctions.validade(str, null, validationType, -1, -1);
            if (validated) {
                jLabel.setForeground(Configurations.getFieldValidationOk());
            } else {
                jLabel.setForeground(Configurations.getFieldValidationNotOk());
                return false;
            }
        } else {
            jLabel.setForeground(Configurations.getFieldValidationOk());
        }
        return true;

    }

    public boolean isIsRequired() {
        return isRequired;
    }

    public JLabel getjLabel() {
        return jLabel;
    }

    public boolean isUpper() {
        return upper;
    }

    public void setIsRequired(boolean isRequired) {
        this.isRequired = isRequired;
    }
}
